package com.itheima.controller;

import com.itheima.anno.LogOperation;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.ClazzQueryParam;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping( "/clazzs")
@RestController

public class ClazzController {
    // 注入业务层
    @Autowired
    private ClazzService clazzService;

    // 查询所有
    @LogOperation
    @GetMapping("list")
    public Result list(){
        List<Clazz> clazzList = clazzService.findAll();
        return Result.success(clazzList);
    }

    @GetMapping
    public Result page(ClazzQueryParam clazzQueryParam){
        PageResult pageResult = clazzService.page(clazzQueryParam);
        return Result.success(pageResult);
    }

    @LogOperation
    @DeleteMapping( "/{id}")
    public Result delete(@PathVariable Integer id){
        System.out.println("根据id删除部门"+ id);
        clazzService.deleteByIds(id);
        return Result.success();
    }

    // 根据id查询
    @LogOperation
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        Clazz clazz = clazzService.getById(id);
        return Result.success(clazz);
    }

    @LogOperation
    @PutMapping
    public Result update(@RequestBody    Clazz clazz){
        clazzService.update(clazz);
        return Result.success();
    }

    @LogOperation
    @PostMapping
    public Result add(@RequestBody Clazz clazz){
        clazzService.save(clazz);
        return Result.success();
    }
}
